﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using BusinessLogicLayer;
using Entities;
using QLNhaSach.Class;

namespace QLNhaSach.Modules
{
    public partial class LapBaoCaoTon : UserControl
    {
        BindingList<BaoCaoTonOBJ> gridDataList = new BindingList<BaoCaoTonOBJ>();
        public LapBaoCaoTon()
        {
            InitializeComponent();
            DateTime now = DateTime.Now;
            string month = now.Month.ToString();
            string year = now.Year.ToString();
            textEdit1.Text = month;
            textEdit2.Text = year;
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            gridDataList.Clear();
            string thang = textEdit1.Text;
            string nam = textEdit2.Text;
            DataTable dt = new DataTable();
            SachBO sbo = new SachBO();
            dt = sbo.layDanhSachSach();
            BaoCaoTon bct = new BaoCaoTon();
            bct.Thang = thang;
            bct.Nam = nam;
            BaoCaoTonBO bctbo = new BaoCaoTonBO();
            int BCT_MaBaoCaoTon = bctbo.InsertBaoCaoTon(bct);
            int i = 1;
            foreach (DataRow row in dt.Rows)
            {
                int maSach = int.Parse(row["S_MASACH"].ToString());
                ChiTietHoaDonBO cthdbo = new ChiTietHoaDonBO();
                int tongSachBan = cthdbo.GetTongSachBanTrongThang(thang, nam, maSach);
                ChiTietPhieuNhapBO ctpnbo = new ChiTietPhieuNhapBO();
                int tongSachNhap = ctpnbo.GetTongSachNhapTrongThang(thang, nam, maSach);
                int tonDau = int.Parse(row["S_SOLUONGTON"].ToString());
                gridDataList.Add(new BaoCaoTonOBJ(i++.ToString(), row["S_TENSACH"].ToString(), row["S_SOLUONGTON"].ToString(), (tongSachNhap - tongSachBan).ToString(), (tongSachNhap - tongSachBan + tonDau).ToString()));
                ChiTietBaoCaoTon ctbct = new ChiTietBaoCaoTon();
                ctbct.MaBaoCaoTon = BCT_MaBaoCaoTon;
                ctbct.MaSach = maSach;
                ctbct.TonDau = tonDau;
                ctbct.PhatSinh = tongSachNhap - tongSachBan;
                ctbct.TonCuoi = tongSachNhap - tongSachBan + tonDau;
                ChiTietBaoCaoTonBO ctbctbo = new ChiTietBaoCaoTonBO();
                ctbctbo.InsertChiTietBaoCaoTon(ctbct);
            }
            gridControl1.DataSource = gridDataList;
            
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            Environment.Exit(1);
        }

        private void textEdit1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsNumber(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                MessageBox.Show("Chỉ nhập kí tự số");
                e.Handled = true;
            }
        }

        private void textEdit2_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsNumber(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                MessageBox.Show("Chỉ nhập kí tự số");
                e.Handled = true;
            }
        }
    }
}
